//
//  CZTabBar.swift
//  Swift -- 微博
//
//  Created by sks on 16/6/26.
//  Copyright © 2016年 mohaha. All rights reserved.
//

import UIKit

//声明协议
protocol CZTabBarDelegate : NSObjectProtocol {

    func composeBtnClick()
}

class CZTabBar: UITabBar {
    
    //声明代理  修饰代理需要继承 NSObjectProtocol 基协议
    weak var CZDelegate :CZTabBarDelegate?
    
   override  init(frame: CGRect) {
        super.init(frame: frame)
    
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        let width = SCREENW / 5
        var index:CGFloat = 0  //记录标识
        
        for value in self.subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.frame.size.width = width
                value.frame.origin.x = index * width
                
                index++
                
                if index == 2 {
                    index++
                }
                
            }
        }
        
        composeBtn.center.x = self.frame.width * 0.5
        composeBtn.center.y = self.frame.height * 0.5
        

    }

    //设置视图
    func setupUI() {
       
        
         addSubview(composeBtn)
        
        
    }
    
    //懒加载按钮
    private lazy var composeBtn: UIButton = {
    
        let button = UIButton()
        
        button.addTarget(self, action: #selector(CZTabBar.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
     return button
        
    }()
    
     @objc private func buttonClick()  {
    
        CZDelegate?.composeBtnClick()
    }
    
}
